package com.jiutong.meipenggang.ui.activitys;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jiutong.meipenggang.R;
import com.jiutong.meipenggang.adapters.AvailableBalanceAdapter;
import com.jiutong.meipenggang.base.BaseActivity;
import com.jiutong.meipenggang.entity.AvailableBalanceBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AvailableBalanceActivity extends BaseActivity {


    @Bind(R.id.available_balance_list)
    RecyclerView availableBalanceList;
    @Bind(R.id.iv_arrow)
    ImageView ivArrow;
    @Bind(R.id.rl_title)
    RelativeLayout rlTitle;
    @Bind(R.id.tv_common_title)
    TextView tvCommonTitle;
    @Bind(R.id.iv_back)
    ImageView ivBack;
    private int popIsShow = 0;// 0 消失  1显示
    private PopupWindow popupWindow;

    @Override
    public int getLayoutResouceId() {
        return R.layout.activity_available_balance;

    }

    @Override
    protected void initView() {
        super.initView();
        ButterKnife.bind(this);
        tvCommonTitle.setText("可用余额");
        initPop();

    }

    private void initPop() {
        popupWindow = new PopupWindow(this);
        popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        View contentView = LayoutInflater.from(this).inflate(R.layout.pop_available_balance, null);
        popupWindow.setContentView(contentView);
        popupWindow.setOutsideTouchable(true);
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        popupWindow.setBackgroundDrawable(dw);
        popupWindow.setFocusable(true);
        popwindowListener(contentView);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                ivArrow.setImageResource(R.mipmap.arrow_down);
                popIsShow = 1;
            }
        });
    }

    @Override
    protected void initDate(Bundle savedInstanceState) {
        super.initDate(savedInstanceState);
        AvailableBalanceBean availableBalanceBean = new AvailableBalanceBean();
        availableBalanceBean.setMoney("-0.02");
        availableBalanceBean.setMessage("到期获得收益");
        availableBalanceBean.setDate("08-18 14:17");
        AvailableBalanceBean availableBalanceBean1 = new AvailableBalanceBean();
        availableBalanceBean1.setMoney("+0.02");
        availableBalanceBean1.setMessage("美财宝获得收益");
        availableBalanceBean1.setDate("08-18 14:17");
        List<AvailableBalanceBean> availableBalancelist = new ArrayList<>();
        availableBalancelist.add(availableBalanceBean);
        availableBalancelist.add(availableBalanceBean1);
        AvailableBalanceAdapter adapter = new AvailableBalanceAdapter(this, R.layout.item_available_blance, availableBalancelist);
        availableBalanceList.setLayoutManager(new LinearLayoutManager(this));
        availableBalanceList.setAdapter(adapter);
    }

    @OnClick({R.id.iv_arrow, R.id.iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_arrow:
                popupWindow.showAsDropDown(rlTitle);
                popIsShow = 1;
                if (popIsShow == 1) {
                    ivArrow.setImageResource(R.mipmap.arrow_up);
                    popIsShow = 0;
                } else if (popIsShow == 0) {
                    popIsShow = 1;
                    ivArrow.setImageResource(R.mipmap.arrow_down);
                }
                break;
            case R.id.iv_back:
                finish();
                break;
        }


    }

    private void popwindowListener(View contentView) {
        final TextView tv_all = (TextView) contentView.findViewById(R.id.tv_all);
        final TextView tv_withdraw = (TextView) contentView.findViewById(R.id.tv_withdraw);
        final TextView tv_project_touzi = (TextView) contentView.findViewById(R.id.tv_project_touzi);
        final TextView tv_in_meicaibao = (TextView) contentView.findViewById(R.id.tv_in_meicaibao);
        final TextView tv_chongzhi = (TextView) contentView.findViewById(R.id.tv_chongzhi);
        final TextView tv_huangben = (TextView) contentView.findViewById(R.id.tv_huangben);
        final TextView tv_shouyi = (TextView) contentView.findViewById(R.id.tv_shouyi);
        final TextView tv_out_meicaibao = (TextView) contentView.findViewById(R.id.tv_out_meicaibao);
        final TextView tv_xianjinjiangli = (TextView) contentView.findViewById(R.id.tv_xianjinjiangli);
        tv_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setBlue(tv_all);
                setWhite(tv_withdraw);
                setWhite(tv_project_touzi);
                setWhite(tv_in_meicaibao);
                setWhite(tv_chongzhi);
                setWhite(tv_huangben);
                setWhite(tv_out_meicaibao);
                setWhite(tv_xianjinjiangli);
                setWhite(tv_shouyi);
                popupWindow.dismiss();
            }
        });
        tv_withdraw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setBlue(tv_withdraw);
                setWhite(tv_all);
                setWhite(tv_project_touzi);
                setWhite(tv_in_meicaibao);
                setWhite(tv_chongzhi);
                setWhite(tv_huangben);
                setWhite(tv_out_meicaibao);
                setWhite(tv_xianjinjiangli);
                setWhite(tv_shouyi);
                popupWindow.dismiss();
            }
        });
        tv_project_touzi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setBlue(tv_project_touzi);
                setWhite(tv_withdraw);
                setWhite(tv_all);
                setWhite(tv_in_meicaibao);
                setWhite(tv_chongzhi);
                setWhite(tv_huangben);
                setWhite(tv_out_meicaibao);
                setWhite(tv_xianjinjiangli);
                setWhite(tv_shouyi);
                popupWindow.dismiss();
            }
        });
        tv_in_meicaibao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setBlue(tv_in_meicaibao);
                setWhite(tv_withdraw);
                setWhite(tv_project_touzi);
                setWhite(tv_all);
                setWhite(tv_chongzhi);
                setWhite(tv_huangben);
                setWhite(tv_out_meicaibao);
                setWhite(tv_xianjinjiangli);
                setWhite(tv_shouyi);
                popupWindow.dismiss();
            }
        });
        tv_chongzhi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setBlue(tv_chongzhi);
                setWhite(tv_withdraw);
                setWhite(tv_project_touzi);
                setWhite(tv_in_meicaibao);
                setWhite(tv_all);
                setWhite(tv_huangben);
                setWhite(tv_out_meicaibao);
                setWhite(tv_xianjinjiangli);
                setWhite(tv_shouyi);
                popupWindow.dismiss();

            }
        });
        tv_huangben.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setBlue(tv_huangben);
                setWhite(tv_withdraw);
                setWhite(tv_project_touzi);
                setWhite(tv_in_meicaibao);
                setWhite(tv_chongzhi);
                setWhite(tv_all);
                setWhite(tv_out_meicaibao);
                setWhite(tv_xianjinjiangli);
                setWhite(tv_shouyi);
                popupWindow.dismiss();
            }
        });
        tv_shouyi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setBlue(tv_shouyi);
                setWhite(tv_withdraw);
                setWhite(tv_project_touzi);
                setWhite(tv_in_meicaibao);
                setWhite(tv_chongzhi);
                setWhite(tv_huangben);
                setWhite(tv_out_meicaibao);
                setWhite(tv_xianjinjiangli);
                setWhite(tv_all);
                popupWindow.dismiss();
            }
        });
        tv_out_meicaibao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setBlue(tv_out_meicaibao);
                setWhite(tv_withdraw);
                setWhite(tv_project_touzi);
                setWhite(tv_in_meicaibao);
                setWhite(tv_chongzhi);
                setWhite(tv_huangben);
                setWhite(tv_all);
                setWhite(tv_xianjinjiangli);
                setWhite(tv_shouyi);
                popupWindow.dismiss();
            }
        });
        tv_xianjinjiangli.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setBlue(tv_xianjinjiangli);
                setWhite(tv_withdraw);
                setWhite(tv_project_touzi);
                setWhite(tv_in_meicaibao);
                setWhite(tv_chongzhi);
                setWhite(tv_huangben);
                setWhite(tv_out_meicaibao);
                setWhite(tv_all);
                setWhite(tv_shouyi);
                popupWindow.dismiss();
            }
        });
    }

    private void setBlue(TextView view) {
        view.setBackgroundResource(R.drawable.shape_bg_pop);
        view.setTextColor(getResources().getColor(R.color.white));
    }

    private void setWhite(TextView view) {
        view.setBackgroundResource(R.drawable.shape_bg_pop_white);
        view.setTextColor(getResources().getColor(R.color.common_blue));
    }

}

